package crivia.ecp.carry.html.form;

import java.util.HashMap;
import java.util.Map;

import crivia.ecp.ECPExecuter;
import crivia.ecp.ECPHtml;
import crivia.ecp.carry.html.DO;
import crivia.ecp.carry.html.X;
import crivia.ecp.common.ECR;
import crivia.ecp.exception.WrongDocumentObject;
import crivia.mvc.a.Executer;

public class Form extends DO {

	public static final String Method_Post = "post";
	public static final String Method_Get = "get";
	
	public static final String Type_FileSender = "multipart/form-data";

	public Form(ECPExecuter page) {
		super();
		basePage = page;
	}

	public Form(ECPExecuter page , String key) {
		super();
		basePage = page;
		this.key = key;
	}

	public Form(ECPExecuter page , String key , String action) {
		super();
		basePage = page;
		this.key = key;
		this.action = action;
	}

	@Override
	protected String name() {
		return "form";
	}

	@Override
	protected String innerHTML() {
		StringBuilder hiddens = new StringBuilder();
		if ("8".equals(ECPExecuter.ep(ECPExecuter.EPK_Version))){
			ECPExecuter ecp = getBasePage();
			if (ecp instanceof ECPHtml){
				if (((ECPHtml)ecp).hasTopTab()){
					addHidden("inTopTab", "1");
				}
			}
		}
		for (Input input : hiddenMap.values()){
			hiddens.append(input);
		}
		return ""
			+ hiddens
			+ (inner == null ? "" : inner.toString());
	}
	
	@Override
	public String toString() {
		addAttr("action", action);
		addAttr("method", method);
		addAttr("id", key);
		addAttr("name", key);
		addAttr("enctype", type);
		addAttr("target", target);
		
		getBasePage();
		String submitScript = ""
		+ "\nfunction " + key + "Submit(tV){"
			+ ECR.ns(getSubmitScript())
			+ "\nif (window._E){"
				+ "\nvar info = _E.editer.check();"
				+ "\nif (info){"
					+ ("8".equals(ECPExecuter.ep(ECPExecuter.EPK_Version))
						? "\n_E.e8o.alert(info.replace(/\\n/g, '<br/>'));" : "\nalert(info);")
					+ "\nreturn;"
				+ "\n}"
			+ "\n}"
			+ "\nvar action = " + (ECR.hasCondition(action) 
					? "'" + action + "';" : "'" + Executer.EU(getBasePage()) + "';")
			+ "\nif (tV) {"
				+ "\naction = tV;"
			+ "\n}"
			+ "\ndocument." + key + ".action = action;"
			+ "\ndocument." + key + ".submit();"
		+ "\n}";
		return super.toString()
		+ X.JavaSctipt(submitScript);
	}
	
	protected ECPExecuter basePage;
	public ECPExecuter getBasePage() {
		if (null == basePage){
			throw new WrongDocumentObject("Form Must Inner Page.");
		}
		return basePage;
	}
	public void setBasePage(ECPExecuter basePage) {
		this.basePage = basePage;
	}
	
	protected Object inner = "";
	public Object getInner() {
		return inner;
	}
	@SuppressWarnings("unchecked")
	public <T> T getInner(Class<T> type) {
		return (T) inner;
	}
	public Form setInner(Object inner) {
		this.inner = inner;
		return this;
	}
	
	protected String action;
	public String getAction() {
		return action;
	}
	public Form setAction(String action) {
		this.action = action;
		return this;
	}
	
	protected String method = Method_Post;
	public String getMethod() {
		return method;
	}
	public Form setMethod(String method) {
		this.method = method;
		return this;
	}
	
	protected String key = "CriviaForm";
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	
	protected String type;
	public String getType() {
		return type;
	}
	public Form setType(String type) {
		this.type = type;
		return this;
	}
	
	protected String target;
	public String getTarget() {
		return target;
	}
	public Form setTarget(String target) {
		this.target = target;
		return this;
	}
	
	protected Map<String, Input> hiddenMap = new HashMap<String, Input>();
	public Input getHidden(String key){
		return hiddenMap.get(key);
	}
	public Map<String, Input> getHiddenMap() {
		return hiddenMap;
	}
	public Form setHiddenMap(Map<String, Input> hiddenMap) {
		this.hiddenMap = hiddenMap;
		return this;
	}
	@Deprecated
	public Form setHidden(String key , String value) {
		hiddenMap.get(key).setValue(value);
		return this;
	}
	public Form addHidden(String key , String value){
		hiddenMap.put(key, new Input(key ,value ,Input.Type_Hidden, null));
		hiddenMap.get(key).isReg = false;
		return this;
	}
	public Form addHidden(String key){
		hiddenMap.put(key, new Input(key ,basePage.rp(key) ,Input.Type_Hidden, null));
		hiddenMap.get(key).isReg = false;
		return this;
	}
	
	protected String submitScript;
	public String getSubmitScript() {
		return submitScript;
	}
	public Form setSubmitScript(String submitScript) {
		this.submitScript = submitScript;
		return this;
	}

}
